﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Web;
using ApiPrototype.Handlers;
using ApiPrototype.Models;

namespace ApiPrototype.APIs
{
    [ServiceContract]
    public class LessenroosterApi
    {
        [WebGet, Authorization]
        public ApiResponse<LessenroosterDto> Get(int studentId, iphlEntities ctx)
        {
            var rooster = new LessenroosterDto();

            rooster.Planningen = ctx.SP_GET_LESSENROOSTER(studentId).ToList();
            
            foreach (PLANNING p in rooster.Planningen)
            {
                if (
                    (
                        from d in rooster.Docenten
                        where d.DOC_ID == p.DOC_ID
                        select d.DOC_ID
                    ).Count() == 0
                )
                {
                    rooster.Docenten.Add(ctx.DOCENT.Where(d => d.DOC_ID == p.DOC_ID).FirstOrDefault());
                }

                if (
                    (
                        from o in rooster.Olods
                        where o.OLO_ID == p.OLO_ID
                        select o.OLO_ID
                    ).Count() == 0
                )
                {
                    rooster.Olods.Add(ctx.OLOD.Where(o => o.OLO_ID == p.OLO_ID).FirstOrDefault());
                }
            }
            
            return new ApiResponse<LessenroosterDto>(rooster);
        }
    }
}